package com.jfm.jfmc.core.icc.handle;

import com.jfm.jfmc.core.constants.TemplateConstants;
import com.jfm.jfmc.core.icc.CollectTaskManage;
import com.jfm.jfmc.core.icc.worker.ResultCalculator;
import com.jfm.jfmc.core.service.IMonitorMetricsService;
import com.jfm.jfmm.collector.entity.collect.CollectParam;
import com.jfm.jfmm.collector.entity.collect.CollectResult;
import com.jfm.jfmm.utils.JavaScriptUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.script.ScriptException;
import java.util.Map;

/**
 * @author DYang
 * @since 2024-10-28 22:47
 *
 * 处理采集结果
 */
@Slf4j
@Component
public class ResultHandle implements CollectorHandle<CollectResult> {
    
    @Autowired
    private IMonitorMetricsService mockMetricsService;

    @Override
    public void handle(CollectResult event) {
        // 添加缓存计算
        ResultCalculator.addCollectResult(event);

        String collectType = event.getResult().getCollectType();
        if (TemplateConstants.COLLECT.equals(collectType)) {
           // 本地计算
            store(event);
        }else {
            // 注册监听器，监听依赖监控项，计算获取数据
            store(event);
        }
    }




    private void store(CollectResult result) {
        if (result == null || result.getResult() == null || result.getResult().getValue() == null) {
            return;
        }
        // 存储数据
//        log.info("设备：{}， 采集指标：{}, 采集值：{}",result.getResult().getDeviceCode(),result.getResult().getMetricCode(),result.getResult().getValue());
        mockMetricsService.store(result);
    }




}
